//
//  WMMusicVCBase+Player.m
//  WholeMusic
//
//  Created by quanxiong on 2018/3/3.
//  Copyright © 2018年 quanxiong. All rights reserved.
//

#import "WMMusicVCBase+Player.h"
#import "WMMusic.h"
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
#import <BlocksKit/BlocksKit.h>

@implementation WMMusicVCBase (Player)

- (void)startPlay:(WMMusic *)music {
    NSURL *url;
    if ([music fileSize] > 0.1) {
        NSLog(@"开始播放:%@", [music filePath]);
        url = [NSURL fileURLWithPath:[music filePath]];
    } else {
        if (music.musicUrl) {
            url = [NSURL URLWithString:music.musicUrl];
        }
    }
    AVPlayer *avPlayer = [AVPlayer playerWithURL:url];
    self.player = avPlayer;
    AVPlayerViewController *playerController = [[AVPlayerViewController alloc] init];
    playerController.player = avPlayer;
    // 试图的填充模式
    playerController.videoGravity = AVLayerVideoGravityResizeAspect;
    // 是否显示播放控制条
    playerController.showsPlaybackControls = YES;
    [self.navigationController pushViewController:playerController animated:YES];
    // 播放
    [avPlayer bk_addObserverForKeyPath:@"status" task:^(AVPlayer *player) {
        switch (player.status) {
            case AVPlayerStatusUnknown:
                break;
            case AVPlayerStatusReadyToPlay:
                self.played = YES;
                [player play];
                break;
            case AVPlayerStatusFailed:
                break;
            default:
                break;
        }
    }];
}

@end
